/**
 * 
 */
package com.link;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

import com.Constant.Contants;

/**
 * @author LvSaiHui {gurnfe@163.com}
 * @version 1.0 <br>
 *          Copyright (C), 2007-2008, ZJUT <br>
 *          This program is protected by copyright laws. <br>
 *          Program Name: LinkDB.java <br>
 *          Date: Feb 8, 2009 <br>
 *          Description:
 */
public class LinkDB {
	// 已访问的 url 集合
	private static Set<String> visitedUrl = new HashSet<String>();
	// 待访问的 url 集合
	private static ArrayList<String> unVisitedUrl = new ArrayList<String>();
	static {
		for (int i = 0; i < Contants.initialUrls.length; i++) {
			unVisitedUrl.add(Contants.initialUrls[i]);
		}
	}
    public static int getUnVisitedUrlSize(){
    	return unVisitedUrl.size();
    }
	public synchronized  static ArrayList<String> getUnVisitedUrl() {
		return unVisitedUrl;
	}
    public synchronized  static Set<String> getVisitedUrl(){
    	return visitedUrl;
    }
	public synchronized  static void addVisitedUrl(String url) {
		visitedUrl.add(url);
	}

	public synchronized  static void removeVisitedUrl(String url) {
		visitedUrl.remove(url);
	}

	public synchronized  static String unVisitedUrlDeQueue() {
		return unVisitedUrl.remove(0);
	}

	// 保证每个 url 只被访问一次
	public synchronized  static void addUnvisitedUrl(String url) {
		if (url != null && !url.trim().equals("") && !visitedUrl.contains(url)
				&& !unVisitedUrl.contains(url))
			unVisitedUrl.add(url);
	}

	public synchronized  static int getVisitedUrlNum() {
		return visitedUrl.size();
	}

	public synchronized  static boolean unVisitedUrlsEmpty() {
		return unVisitedUrl.isEmpty();
	}
}